package com.example.ujsfish.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


import com.example.ujsfish.R;
import com.example.ujsfish.utils.AppNetService;
import com.example.ujsfish.model.manual.SemiManualCtrlData;
import com.example.ujsfish.utils.DeviceName;
import com.example.ujsfish.utils.RetrofitManager;
import com.example.ujsfish.view.M;
import com.example.ujsfish.view.MyView;


import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class CmSemiAuto extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    private static final String TAG = "CmSemiAuto";
    private Spinner spinner1;
    private Spinner spinner2;
    private Spinner spinner3;
    private Spinner spinner4;
    private Button reset;
    private Button btn_toManual;
    private Button btn_toAuto;
    private Button btn_stop;
    private TextView inputX;
    private TextView inputY;

    private List<String> data_list1;
    private List<String> data_list2;
    private List<String> data_list3;
    private List<String> data_list4;

    private ArrayAdapter<String> arr_adapter1;
    private ArrayAdapter<String> arr_adapter2;
    private ArrayAdapter<String> arr_adapter3;
    private ArrayAdapter<String> arr_adapter4;

    private M mv;

     String paoPan = "0";
     String zhenDong= "0";
     String bengLiuLiang = "0";
     String yaoYeFa = "0";

    String id = DeviceName.getDeviceInstance().getDeviceId().get(0);

    RetrofitManager myRetrofitManager = RetrofitManager.getInstance();
    Retrofit myRetrofit = RetrofitManager.getRetrofit();
    AppNetService appNetService = myRetrofitManager.getApiService(AppNetService.class);


    private SemiManualCtrlData semiManualCtrlData = SemiManualCtrlData.getSemiManualCtrlDataInstance();
    private SemiManualCtrlData.SemiAutoBody semiAutoBody = SemiManualCtrlData.getManualBody();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cm_semif_auto_activity);
        initSpinner();
        reset = findViewById(R.id.btn_reset);
        inputX = findViewById(R.id.inputX);
        inputY = findViewById(R.id.inputY);
        btn_stop = findViewById(R.id.btn_stop);
        btn_toAuto = findViewById(R.id.btn_semiToAuto);
        btn_toManual = findViewById(R.id.btn_semiToManual);

        ConstraintLayout constraintLayout = findViewById(R.id.layoutMy);
        mv = new M(this);

        //constraintLayout.addView(mv);//添加自定义视图

        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                semiAutoBody.setPara(inputX.getText().toString(),inputY.getText().toString(),paoPan,zhenDong,bengLiuLiang,yaoYeFa,id);
                Log.d(TAG, "sendManualCm: " + semiAutoBody.toString());
                Call<ResponseBody> responseBodyCall = appNetService.postSemiAutoControl(semiAutoBody);
                responseBodyCall.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        Toast.makeText(getApplication(),"设置成功",Toast.LENGTH_SHORT);
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                        Toast.makeText(getApplication(),"设置失败",Toast.LENGTH_SHORT);
                    }
                });

            }
        });

        btn_toManual.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CmSemiAuto.this, CmManual.class);
                startActivity(intent);
            }
        });

        btn_toAuto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CmSemiAuto.this, CmAuto.class);
                startActivity(intent);
            }
        });

        btn_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Call<ResponseBody> call = appNetService.postStop(id);
                call.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        Log.d(TAG, "onResponse: " + response);
                    }
                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                        Log.d(TAG, "onFailure: " + t.toString());
                    }
                });
            }
        });



    }



    private void initSpinner() {

        spinner1 = findViewById(R.id.spinner_paopan);
        spinner2 = findViewById(R.id.spinner_zhendong);
        spinner3 = findViewById(R.id.spinner_bengliuliang);
        spinner4 = findViewById(R.id.spinner_yaoyefa);
        //抛盘电机
        //多种不同的模式
        data_list1 = new ArrayList<String>();//data_list是一个数组列表
        data_list1.add("00.0");
        data_list1.add("10.0");
        data_list1.add("20.0");
        data_list1.add("30.0");
        data_list1.add("40.0");
        data_list1.add("50.0");
        data_list1.add("60.0");
        data_list1.add("70.0");
        data_list1.add("80.0");
        data_list1.add("90.0");


        //适配器
        arr_adapter1= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data_list1);
        //设置样式
        arr_adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner1.setAdapter(arr_adapter1);
        //为Spinner设置选中事件监听器
        spinner1.setOnItemSelectedListener(CmSemiAuto.this);

        //振动电机
        //多种不同的模式
        data_list2 = new ArrayList<String>();//data_list是一个数组列表
        data_list2.add("00.0");
        data_list2.add("10.0");
        data_list2.add("20.0");
        data_list2.add("30.0");
        data_list2.add("40.0");
        data_list2.add("50.0");
        data_list2.add("60.0");
        data_list2.add("70.0");
        data_list2.add("80.0");
        data_list2.add("90.0");

        //适配器
        arr_adapter2= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data_list2);
        //设置样式
        arr_adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner2.setAdapter(arr_adapter2);
        //为Spinner设置选中事件监听器
        spinner2.setOnItemSelectedListener(CmSemiAuto.this);

        //泵流量
        //多种不同的模式
        data_list3 = new ArrayList<String>();//data_list是一个数组列表
        data_list3.add("00.0");
        data_list3.add("10.0");
        data_list3.add("20.0");
        data_list3.add("30.0");
        data_list3.add("40.0");
        data_list3.add("50.0");
        data_list3.add("60.0");
        data_list3.add("70.0");
        data_list3.add("80.0");
        data_list3.add("90.0");









        //适配器
        arr_adapter3= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data_list3);
        //设置样式
        arr_adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner3.setAdapter(arr_adapter3);
        //为Spinner设置选中事件监听器
        spinner3.setOnItemSelectedListener(CmSemiAuto.this);

        //药液阀
        //多种不同的模式
        data_list4 = new ArrayList<String>();//data_list是一个数组列表
        data_list4.add("00.0");
        data_list4.add("10.0");
        data_list4.add("20.0");
        data_list4.add("30.0");
        data_list4.add("40.0");
        data_list4.add("50.0");
        data_list4.add("60.0");
        data_list4.add("70.0");
        data_list4.add("80.0");
        data_list4.add("90.0");



        //适配器
        arr_adapter4= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data_list4);
        //设置样式
        arr_adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner4.setAdapter(arr_adapter4);
        //为Spinner设置选中事件监听器
        spinner4.setOnItemSelectedListener(CmSemiAuto.this);



    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

        String number = adapterView.getItemAtPosition(i).toString();
        TextView tv1;
        TextView tv2;
        TextView tv3;
        TextView tv4;
        switch (adapterView.getId()){
            case R.id.spinner_paopan:
                paoPan = number;
                tv1 = (TextView)view;
                tv1.setTextColor(getResources().getColor(R.color.colorBlack));
                break;
            case R.id.spinner_zhendong:
                zhenDong = number;
                //为了确保在振动电机开启的时候，抛盘电机是运行的，防止饵料堵塞出料口
                //即，抛盘电机先于振动电机先运行
                if("0.00".equals(paoPan) && !"0.00".equals(zhenDong)){
                    //当振动电机开启的时候，没有手动开启抛盘电机的话，就自动给一个抛盘电机占空比
                    paoPan = zhenDong;
                    spinner1.setSelection(2,true);
                }
                tv2 = (TextView)view;
                tv2.setTextColor(getResources().getColor(R.color.colorBlack));
                break;
            case R.id.spinner_bengliuliang:
                bengLiuLiang = number;
                tv3 = (TextView)view;
                tv3.setTextColor(getResources().getColor(R.color.colorBlack));
                break;
            case R.id.spinner_yaoyefa:
                yaoYeFa = number;
                tv4 = (TextView)view;
                tv4.setTextColor(getResources().getColor(R.color.colorBlack));
                break;
            default:break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }


    @Override
    protected void onResume() {
        super.onResume();


    }

    @Override
    protected void onPause() {
        super.onPause();
    }


}